package com.twu.biblioteca;

import java.io.*;

public class ConsoleInterface {
    private PrintStream printStream;
    private BufferedReader reader;
    private Menu menu;

    public ConsoleInterface(PrintStream print, BufferedReader read) {

        printStream = print;
        reader = read;
        menu = new Menu();
    }

    public void run() {
        Message welcome = new Message("Welcome");
        printStream.println(welcome.getWelcomeMessage());
        printStream.println(menu.getMenuOptions());

        String line;
        InputStreamReader converter = new InputStreamReader(System.in);
//        BufferedReader reader = new BufferedReader(converter);
        try {

            line = reader.readLine();
            while (!(line.equals("exit"))) {

                String output = menu.selectOption(line);
                printStream.println(output);

                if(line.equals("2") && menu.getCurrentUser() != null) {
                    printStream.println("Enter Book Title: ");
                    String title = reader.readLine();
                    printStream.println(menu.reserveBook(title));

                }

                if(line.equals("4"))
                {
                    printStream.println("Enter username");
                    String username = reader.readLine();
                    username = username.replace("-", "");
                    printStream.println("Enter password");
                    String password = reader.readLine();


                    if(!menu.login(username, password))
                        printStream.println("ERROR: User '" + username + "' could not be logged in");
                }

                printStream.println("\n" + menu.getMenuOptions());
                line = reader.readLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Menu getMenu() {
        return menu;
    }
}